package TopK;
import java.util.ArrayList;
import java.util.Collections;

public class ComparissonTree 
{
	public CompareNode root;
	public ArrayList<CompareNode> leaves;

	public ComparissonTree(CompareNode r)
	{
		root = r;
		leaves = new ArrayList<CompareNode>();
		leaves.add(root);
	}
	
	public ComparissonTree()
	{
		
	}
	
	public CompareNode getRoot() 
	{
		return root;
	}

	public void setRoot(CompareNode root) 
	{
		this.root = root;
	}

	public ArrayList<CompareNode> getLeaves() 
	{
		return leaves;
	}

	public void setLeaves(ArrayList<CompareNode> leaves) 
	{
		this.leaves = leaves;
	}

	public void BuildLeaves (CompareNode v)
	{
		int n = v.getIndices().length;
		CompareNode [] children = new CompareNode [n]; 
		
		for (int i = 0; i < n; i++) 
		{
			int [] childIndices = v.getIndices();
			childIndices[i]++;
			children[i] = new CompareNode();
			children[i].setIndices(childIndices);
			leaves.add(children[i]);
		}
		
		v.setChildren(children);
		leaves.remove(v);
		Collections.sort(leaves);
	}
	
	
	public static class CompareNode extends Node implements Comparable<CompareNode>
	{
		int indices [];
		DerivationTree Tree; 
		
		public DerivationTree getTree() 
		{
			return Tree;
		}

		public void setTree(DerivationTree tree) 
		{
			Tree = tree;
		}

		public int[] getIndices() 
		{
			return indices;
		}

		public void setIndices(int[] indices) 
		{
			this.indices = indices;
		}

		@Override
		public int compareTo(CompareNode other) {
			double w1= weight;
			double w2= other.getWeight();
		    int retVal;
		    if (w1 == w2) { retVal = 0; }	
		    else if (w1 > w2) { retVal = 1; }
		    else {retVal = -1;}
		    return retVal;
		}
	}
}
